package com.l024.meet.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.l024.framework.base.BaseBackActivity;
import com.l024.framework.base.BaseUIActivity;
import com.l024.framework.bmob.BmobManager;
import com.l024.framework.bmob.IMUser;
import com.l024.framework.manager.PermissionManager;
import com.l024.framework.model.AddFriendModel;
import com.l024.framework.utils.CommonUtils;
import com.l024.framework.utils.Logutils;
import com.l024.meet.R;
import com.l024.meet.adapter.AddFriendAdapter;
import com.yanzhenjie.permission.runtime.Permission;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
* @Author  wu_ming_zhi_bei
* @Date   2020/3/6 15:41
* @Notes  好友搜索
*/
public class AddFriendActivity extends BaseBackActivity implements View.OnClickListener {

    /**
     * 1.模拟数据
     * 2.根据条件查询
     * 3.推荐好友
     */

    private LinearLayout llToContact;
    private EditText etPhone;
    private ImageView ivSearch;
    private RecyclerView mSearchResultView;
    private RelativeLayout emptyLayout;

    private AddFriendAdapter addFriendAdapter;
    private List<AddFriendModel> friendModels;
    private String[] permissions = {Permission.READ_EXTERNAL_STORAGE,Permission.WRITE_EXTERNAL_STORAGE,Permission.READ_CONTACTS};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);
        initView();
    }

    private void initView() {
        llToContact = (LinearLayout) findViewById(R.id.ll_to_contact);
        etPhone = (EditText) findViewById(R.id.et_phone);
        ivSearch = (ImageView) findViewById(R.id.iv_search);
        mSearchResultView = (RecyclerView) findViewById(R.id.mSearchResultView);
        emptyLayout = (RelativeLayout) findViewById(R.id.include_empty_view);
        ivSearch.setOnClickListener(this);
        llToContact.setOnClickListener(this);


        friendModels = new ArrayList<>();
        //初始化数据列表
        mSearchResultView.setLayoutManager(new LinearLayoutManager(this));
        mSearchResultView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        addFriendAdapter = new AddFriendAdapter(this,friendModels);
        mSearchResultView.setAdapter(addFriendAdapter);
        addFriendAdapter.setOnClickListener(new AddFriendAdapter.OnClickListener() {
            @Override
            public void OnClick(int position) {
                AddFriendModel addFriendModel = friendModels.get(position);
                if(addFriendModel.getType()==AddFriendAdapter.TYPE_CONTENT){
                    UserInfoActivity.startActivity(AddFriendActivity.this,addFriendModel.getUserId());
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_to_contact:{
                //通讯录获取
                checkPermission();
                break;
            }
            case R.id.iv_search:{
                //根据手机号查询
                queryPhoneFriend();
                break;
            }
        }
    }

    /**
     * 通过电话号码查询
     */
    private void queryPhoneFriend(){
        String phone = etPhone.getText().toString().trim();
        if(TextUtils.isEmpty(phone)){
            Toast.makeText(this, getString(R.string.text_login_phone_null), Toast.LENGTH_SHORT).show();
            return;
        }
        //根据手机号查询好友信息
        BmobManager.getInstance().queryPhoneFriend(phone, new FindListener<IMUser>() {
            @Override
            public void done(List<IMUser> list, BmobException e) {
                if(e==null){
                    if(CommonUtils.isEmpty(list)){
                        System.out.println("搜索到"+list.size()+"好友");
                        Logutils.i(list.get(0).toString());
                        emptyLayout.setVisibility(View.GONE);
                        mSearchResultView.setVisibility(View.VISIBLE);
                        //每次搜索清除数据
                        if(CommonUtils.isEmpty(friendModels)){
                            friendModels.clear();
                        }
                        addTitle("搜索结果");
                        //添加搜索结果
                        for(IMUser imUser:list){
                            addContent(imUser);
                        }
                        //添加推荐好友
                        addTitle("推荐好友");
                        getRecommendFriends();
                        addFriendAdapter.notifyDataSetChanged();
                    }else{
                        emptyLayout.setVisibility(View.VISIBLE);
                        mSearchResultView.setVisibility(View.GONE);
                    }
                }else {
                    Logutils.e("好友查询失败"+phone+"-------------"+e.getMessage());
                }
            }
        });
    }

    /**
     * 添加头部标题
     * @param title
     */
    private void addTitle(String title){
        AddFriendModel addFriendModel = new AddFriendModel();
        addFriendModel.setType(AddFriendAdapter.TYPE_TITLE);
        addFriendModel.setTitle(title);
        friendModels.add(addFriendModel);
    }
    /**
     * 添加好友列表
     * @param imUser
     */
    private void addContent(IMUser imUser){
        AddFriendModel addFriendModel = new AddFriendModel();
        addFriendModel.setType(AddFriendAdapter.TYPE_CONTENT);
        addFriendModel.setUserId(imUser.getObjectId());
        addFriendModel.setPhoto(imUser.getPhoto());
        addFriendModel.setSex(imUser.isSex());
        addFriendModel.setAge(imUser.getAge());
        addFriendModel.setNickName(imUser.getNickName());
        addFriendModel.setDesc(imUser.getDesc());
        friendModels.add(addFriendModel);
    }

    /**
     * 查询推荐好友 模糊查询好友50个
     */
    private void getRecommendFriends(){
        BmobManager.getInstance().queryAllUser(new FindListener<IMUser>() {
            @Override
            public void done(List<IMUser> list, BmobException e) {
                if(e==null){
                    if(CommonUtils.isEmpty(list)){
                        for (IMUser imUser:list){
                            addContent(imUser);
                        }
                        addFriendAdapter.notifyDataSetChanged();
                    }else{
                        Logutils.e("查询推荐好友列表为null");
                    }
                }else{
                    Logutils.e("查询推荐好友失败:错误信息："+e.getMessage());
                }
            }
        });
    }

    /**
     * 申请权限
     */
    private void checkPermission(){
        PermissionManager.requestPermission(this, new PermissionManager.PermissionQuestListener() {
            @Override
            public void onGranted() {
                Logutils.i("权限申请成功");
                startActivity(new Intent(AddFriendActivity.this,ContactFriendActivity.class));
            }

            @Override
            public void onDenied(List<String> data) {
                Logutils.e("权限申请失败");
            }
        },permissions);
    }

}
